<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="Student"  :wrapperCol="wrapperCol" prop="studentName">
              <JSelectStudent  :buttons="false" :course-id="this.courseId" @select="studentInput" v-model="model.studentName" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import JSelectStudent from '../../person/student/JSelectStudent'

  export default {
    name: 'CourseStudentInfoForm',
    components: {
       JSelectStudent
    },
    props: {
      //课程ID
      courseId: {
        type:Number
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 22 },
        },
        form : {
          layout: 'vertical',
        },
        confirmLoading: false,
        validatorRules: {
          studentName: [
              { required: true, message: 'Please select student information!'},
           ]
        },
        url: {
          addStudent: "/sistersophie/courseStudentInfo/addStudent",
          add: "/sistersophie/courseStudentInfo/add",
          edit: "/sistersophie/courseStudentInfo/edit",
          queryById: "/sistersophie/courseStudentInfo/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      studentInput(rows){
        console.info('rows');
        console.info(rows);
        if(rows.length > 0 ){
          this.model.students = rows;
        }else{
          this.model.students = [];
        }
        console.info(this.model.students);
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        this.model.courseId = this.courseId;
        console.info('this.model.courseId' +this.model.courseId);
        console.info('this.model.students' +this.model.students.length);
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = this.url.addStudent;
            let method = 'post';
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>